<?php


namespace app\admin\controller;

use app\common\lib\Auth;
use think\Cache;
use think\Db;
use think\Model;
use think\Request;
use app\common\lib;

class Student extends Base
{
    public function index()
    {
        if(request()->isAjax()) {
           /**/
            $xlsx_url = Request::instance()->param('msg_url');  //完整xlsx路径
            $xlsx_dir = Request::instance()->param('dir');  //目录路径，需删除
//            return show('200',$xlsx_url,['url'=>'http://nxist.crukou.com/'.$xlsx_url,'dir' => $xlsx_dir]);
//            die;
            //调用导入excel方法
            $data = $this->importExcel($xlsx_url);
            $length = count($data); //增加创建时间和修改时间
            $sql = [];
            for ($i = 1; $i < $length; $i++) { //除去表头字段
                $sql[$i]['id']          = $data[$i][0];//学号可控，数据库自增
                $sql[$i]['advice_num']  = $data[$i][1];//录取证号
                $sql[$i]['stu_name']    = $data[$i][2];//姓名
                $sql[$i]['stu_num']     = $data[$i][3];//准考证号
                $sql[$i]['stu_card']    = $data[$i][4];//身份证号
                $sql[$i]['major']       = $data[$i][5];//专业
                $sql[$i]['sec_level']   = $data[$i][6];//二级学院
                $sql[$i]['stu_year']    = $data[$i][7];//录取年份
                $sql[$i]['province']    = $data[$i][8];//生源地
                $sql[$i]['status']      = $data[$i][9];//状态
                $sql[$i]['type']        = $data[$i][10];//1 本科 2 专科
            }
            try{
                //写入数据库
                $res = Model('Student')->insertAll($sql);
            }catch (\Exception $e){
                return show('200',$e->getMessage(),'0');
            }
            if(!$res) {
                return show('200','没有任何有用的数据导入','0');
            }
            del_dir($xlsx_dir); //删除文件夹，及其附带子文件
            $length--;
            return show('200',$length.'行记录 导入成功！','1');
        }
        /*************************检测权限 开始***********************/
        $auth = new Auth();
        $request = Request::instance();
        if(!$auth->check($request->path(),$this->isLogin()->id)) {
            return $this->redirect('not_rule/index/index');
        }
        /*************************检测权限 结束***********************/
        return $this->fetch();
    }

    public function stu_all()
    {
        if(request()->isAjax()) {
            extract(input()); //将接收的参数赋值给变量
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $map = array();
            if(isset($stu_year) && $stu_year!="")
            {
                //学年为必选项
                $map['stu_year'] = $stu_year;
                //关键字查询
                if(isset($keyword) && $keyword!="")
                {
                    $map['stu_name'] = ['like',"%" . $keyword . "%"];
                }
                //报道状态
                if(isset($status) && $status!="")
                {
                    $map['status'] = $status;
                }
                //学历筛选
                if(isset($type) && $type!="")
                {
                    $map['type'] = $type;
                }
                //开始筛选
                try {
                    $count = Db::name('student')->where($map)->cache(true,'3600')->count();  //总条目数据
                    $lists = Db::name('student')->page($NowPage,$limits)->where($map)->cache(true,'600')->select();  //当前页
                }catch (\Exception $e) {
                    return json(['code' => 500, 'msg' => $e->getMessage()]);
                }
                return json(['code' => 200, 'msg' => '', 'count' => $count, 'data' => $lists]);
            }
            //非学年数据筛选
            return json(['code' => 200, 'msg' => '学年 为必填项', 'count' => '0', 'data' => []]);
        }
        /*************************检测权限 开始***********************/
        $auth = new Auth();
        $request = Request::instance();
        if(!$auth->check($request->path(),$this->isLogin()->id)) {
            return $this->redirect('not_rule/index/index');
        }
        /*************************检测权限 结束***********************/
        $stu_year = Model('SchoolYear')->get_status_all_msg('1');
        $this->assign([
            'stu_year' => $stu_year
        ]);
        return $this->fetch();
    }

    public function stu_all_add()
    {
        if (request()->isAjax()) {
            $data = input('post.');
            try{
                $_add_res = Model('Student')->save($data);
            }catch (\Exception $e) {
                return show('200',$e->getMessage(),'0');
            }
            if(!$_add_res) {
                return show('200','数据库错误','0');
            }
            Cache::clear(); //删除缓存重新加载
            return show('200','添加成功，刷新父页面生效','1');
        }else{
            $stu_year = Model('SchoolYear')->get_status_all_msg('1');
            $this->assign([
                'stu_year' => $stu_year
            ]);
            return $this->fetch();
        }
    }

    public function stu_all_edit()
    {
        if (\request()->isAjax()){
            $data = input('post.');
            try{
                $_edit_res = Model('Student')->save($data,['id'=>$data['id']]);
            }catch (\Exception $e) {
                return show('200',$e->getMessage(),'0');
            }
            if(!$_edit_res) {
                return show('200','数据库错误','0');
            }
            return show('200','修改成功，刷新父页面生效','1');
        }else {
            $id = Request::instance()->param('id');
            $this_data = Model('Student')->get(['id' => $id]);
            $this->assign([
                'this_data' => $this_data,
            ]);
            $this->stu_all_add();
            return $this->fetch();
        }
    }

    public function stu_all_del()
    {
        if(request()->isAjax()) {
            $id = Request::instance()->param('id');
            try{
                $res = Model('Student')->del_msg($id);
            }catch (\Exception $e){
                return show('200',$e->getMessage(),'0');
            }
            if (!$res) {
                return show('200','数据库错误','0');
            }
            Cache::clear(); //删除缓存重新加载
            return show('200','删除成功！','1');
        }else{
            return show('500','非法操作');
        }
    }
}